//取得小数
function round(num,offset){ 
	var _num=num;
	var n=num.toString().split('.');
	try{ 
		num=n[0]+'.'+n[1].substr(0,offset);
	}catch(e){
		num=_num;
	}
	return num;
}
$(function(){ 
	var context;
	var app="reserve.php";
	var total_goods_price=0;
	var total_bonus_price=0;
	//分页
	page(app+'?is_ajax=1&act=bonus_reserve');
	function page(url){ 
		$.post(url,function(data){ 
			$('#reserve_right').html(data.content);
			context=$('#reserve_goods_list');			
			$('.pagebar a',context).click(function(){
				$(this).click(function(){return false;});
				page($(this).attr('href'));
				return false;
			});	
			$('#reserve_search_type a,#reserve_search_btn').click(function(){ 
				var at=$(this).parent().attr('id');
				var keywords=$.trim($('#keywords').val());
				var start_price=parseFloat($('#start_price').val());
				var end_price=parseFloat($('#end_price').val());
				var url="";
				var table=[ 
					['reserve_search_cat',		'cid'],
					['reserve_search_subcat',	'subcid'],
					['reserve_search_price',	'price'],
					['reserve_search_brand',	'brand'],
					['reserve_search_bonus',	'bonus_type']
				];
				
				for(var i=0;i<table.length;i++){ 
					if(at==table[i][0]){ 
						url+='&'+table[i][1]+'='+($(this).attr(table[i][1])||0);
					}else{ 
						url+='&'+table[i][1]+'='+($('#'+table[i][0]+' a.curr').attr(table[i][1])||0);
					}
				}
				url+='&keywords='+$.trim($('#keywords').val())
					+'&start_price='+parseFloat($('#start_price').val())
					+'&end_price='+parseFloat($('#end_price').val());
				
				url=app+'?is_ajax=1&act=bonus_reserve'+url;
				page(url);
			});
			$('#reserve_goods_list tbody tr:odd').addClass('odd');		
			style_reset();
			$('#reserve_goods_list .select_btn').click(function(){
				var data=$(this).attr('data');
				var $data=$.parseJSON(data);
				//检查是否添加合适的套餐卡
				var flag_card=0;//套餐卡未添加
				var card_num=$('#reserve_card_table tbody tr[bonus_type_id="'+$data.bonus_type_id+'"]').size();
				var card_goods_num=$('#reserve_goods_table tbody tr[bonus_type_id="'+$data.bonus_type_id+'"]').size();
				
				if(card_num>0){ 
					flag_card=1;//套餐卡已添加,商品未添加
					if(card_num==card_goods_num)
						flag_card=2;//商品已添加
				}
				switch(flag_card){ 
					case 0:
						alert("\""+$data.bonus_name+"套餐卡\"未添加,请添加套餐卡.");return;break;
					case 2:
						alert("改套餐已选择了商品,请继续添加套餐卡.");return;break;
				}
				
				if($('#reserve_goods_table tbody tr[goods_id="'+$data.goods_id+'"]').size()>0&&false){ //已添加
					alert('已添加.');
				}else{
					var seg='<tr goods_id="'+$data.goods_id+'" bonus_type_id="'+$data.bonus_type_id+'"\
						data=\''+data+'\'>\
					<td><a href="goods.php?id='+$data.goods_id+'" target="_blank">\
						<img src="'+$data.goods_img+'" class="goods_img"/>\
						</a></td>\
					<td><a href="goods.php?id='+$data.goods_id+'" target="_blank">'+$data.goods_name+'</a></td>\
					<td>'+$data.goods_price+'</td>\
					<td>'+$data.bonus_name+'</td>\
					<td><div class="del"></div></td></tr>';
					$('#reserve_goods_table tbody').append(seg);
					$('#reserve_goods_table .del').click(function(){ 
						$(this).parent().parent().remove();
						assign_goods_hidden();
					});
				}
				assign_goods_hidden();
				$('#reserve_goods_table .goods_num').blur(function(){ 
					var num=parseInt($(this).val())>0?parseInt($(this).val()):1;
					$(this).val(num);
					assign_goods_hidden();
				});
			});
		},'json');		
	}	
	$('input[name="ship_by_self"]').click(function(){ 
		style_reset();
	});
	//保证左右高度相等
	function style_reset(){ 
		var arr=[$('#reserve_left'),$('#reserve_right'),$('#reserve_left form')];
		for(var i=1;i<arr.length;i++){ 
			arr[i].css({'height':'auto'});
		}
		//console.log(arr);
		var height=arr[0].height();
		for(var i=1;i<arr.length;i++){ 
			height=height<arr[i].height()?arr[i].height():height;
		}
		$('#reserve_left,#reserve_right').height(height);
	}
	$('#bonus_reserve_add_btn').click(function(){ 
		var card=$('#bonus_reserve_add_text').val();
		//检查是否已添加
		var card_list=get_card_list();
		for(var i=0;i<card_list.length;i++){ 
			if(card==card_list[i].bonus_sn){ 
				alert('已经添加了.');
				return;
			}
		}
		$.post(app,{act:'add_bonus',card:card},function(data){ 
			if(parseInt(data.error)>0){ 
				alert(data.message);
			}else{
				var data=data.content;
				$data=$.parseJSON(data);
				$('#reserve_card_table tbody').append('<tr bonus_type_id="'+$data.bonus_type_id+'" \
					data=\''+data+'\'>\
					<td>'+$data.bonus_sn+'</td>\
					<td>'+$data.bonus_name+'</td>\
					<td><div class="del"></div></td></tr>');
				$('#reserve_card_table .del').click(function(){ 
					$(this).parent().parent().remove();
					assign_bonus_hidden();
				});
				assign_bonus_hidden();
				page(app+'?is_ajax=1&act=bonus_reserve&bonus_type='+$data.bonus_type_id);
			}
		},'json');
	});
	
	function get_card_list(){ 
		var card_list=new Array();
		$('#reserve_card_table tbody tr').each(function(){ 
			card_list.push($.parseJSON($(this).attr('data')));
		});
		return card_list;
	}
	
	function assign_goods_hidden(){ 
		var goods_list=new Array();
		var total_num=0;
		var total_price=0.0;
		var total_num=$('#reserve_goods_table tbody tr').size();
		$('#reserve_goods_table tbody tr').each(function(){ 
			$goods=$.parseJSON($(this).attr('data'));
			goods_list.push($goods);
			total_price+=parseFloat($goods.goods_price);
		});
		assign_hidden('goods_hidden',goods_list);
		total_goods_price=total_price;
		$('#goods_summary .num').html(total_num);
		$('#card_summary .card_price').html(round(total_price,2));
		assign_bonus_hidden();
	}
	assign_goods_hidden();
	function assign_bonus_hidden(){ 
		assign_hidden('bonus_hidden',get_card_list());
		style_reset();
	}
	function assign_hidden(name,val){ 
		if($('#'+name).size()==0){ 
			$('#bonus_form').append('<input type="hidden" name="'+name+'" id="'+name+'" value=""/>');
		}
		$('#'+name).val(val.toJSONString());		
	}
});
function check_bonus_reserve_from(){
	var goods_num=$.parseJSON($('#goods_hidden').val()).length;
	var card_num=$.parseJSON($('#bonus_hidden').val()).length;
	
	if(goods_num==0){ 
		alert('请选择商品.');
		return false;
	}
	if(card_num==0){ 
		alert('请添加套餐卡.');
		return false;
	}
	if(goods_num<card_num){
		alert('请删去多余的套餐卡!');
		return false;
	}
	if($('input[name="agency_id"]:checked').size()==0&&$('input[name="address_id"]:checked').size()==0){
		alert('请确认收货信息.');
		return false;
	}
	return true;
}
